import {getStore} from "../../../common";

const state = () => ({
    list: [],
    leafMenuList: [],
    elementList: [],
    currentMenu: '',
    allMenus:[]
})

const getters = {}

const actions = {
    resetMenu({commit}){
        commit('resetMenu')
    },
    queryMenuList({commit}, payload){
        commit('queryMenuList', payload)
    },
    setMenus({commit}, payload){
        commit('setMenus', payload)
    },
    setCurrentMenu({commit},payload){
        commit('setCurrentMenu',payload)
    }
}

const mutations = {
    resetMenu(state){
        state.list = []
        state.leafMenuList = []
        state.elementList = []
        state.currentMenu = null
    },
    queryMenuList(state, {data, callback}){
        // data = data.sort((a, b) => parseInt(a.uumsFunctionEntity.displayOrder) - parseInt(b.uumsFunctionEntity.displayOrder));
        // 渲染菜单
        const list = data.filter(item => item.uumsFunctionEntity.functionType == "FUNCTION_TYPE_MODULE").map(item => {
            return {
                ...item,
                name: item.uumsFunctionEntity.functionName,
                code: item.uumsFunctionEntity.functionCode,
                children: []
            }
        });
        const childList = data.filter(item => item.uumsFunctionEntity.functionType == "FUNCTION_TYPE_FUNCTION").map(item => {
            return {
                ...item,
                name: item.uumsFunctionEntity.functionName,
                code: item.uumsFunctionEntity.functionCode,
                children: [],
                path: item.uumsFunctionEntity.functionUrl.split('#')[1] || item.uumsFunctionEntity.functionUrl.split('#')[0]
            }
        })
        childList.forEach(childItem => {
            const index = list.findIndex(item => item.code == childItem.uumsFunctionEntity.parentFunction)
            list[index] && list[index].children && (list[index].children).push(childItem)
        })
        const elementList = data.filter(item => item.uumsFunctionEntity.functionType == 'FUNCTION_TYPE_ELEMENT').map(item => {
            return {
                ...item,
                name: item.uumsFunctionEntity.functionName,
                code: item.uumsFunctionEntity.functionCode,
                path: item.uumsFunctionEntity.functionUrl.split('#')[1] || item.uumsFunctionEntity.functionUrl.split('#')[0]
            }
        })
        elementList.forEach(elementItem => {
            const index = childList.findIndex(item => item.code == elementItem.uumsFunctionEntity.parentFunction)
            childList[index] && childList[index].children && (childList[index].children).push(elementItem)
        })
        state.list = list.sort((a, b) => parseInt(a.uumsFunctionEntity.displayOrder) - parseInt(b.uumsFunctionEntity.displayOrder))
        state.leafMenuList = childList.sort((a, b) => parseInt(a.uumsFunctionEntity.displayOrder) - parseInt(b.uumsFunctionEntity.displayOrder))
        state.elementList = elementList.sort((a, b) => parseInt(a.uumsFunctionEntity.displayOrder) - parseInt(b.uumsFunctionEntity.displayOrder))
        callback && callback()
    },
    setMenus(state, {menu, callback}){
        let currentRole = JSON.parse(getStore('currentRole'));        
        const mapMenus  = {
            'manager':[
                {
                    code:'dataManage',
                    name:'数据管理',
                    index:1,
                    url:'/home/dataManage'
                },
                {
                    code:'personnelManage',
                    name:'人员管理',
                    index:2,
                    url:'/home/personnelManage'
                },
                {
                    code:'recipeManage',
                    name:'配方管理',
                    index:3,
                    url:'/home/recipeManage'
                },
                {
                    code:'materialInfo',
                    name:'物料信息',
                    index:4,
                    url:'/home/materialInfo'
                },
                {
                    code:'productManage',
                    name:'产品管理',
                    index:5,
                    url:'/home/productManage'
                },
                {
                    code:'materialManage',
                    name:'工站管理',
                    index:6,
                    url:'/home/materialManage'
                },
                {
                    code:'deviceManage',
                    name:'设备管理',
                    index:7,
                    url:'/home/deviceManage'
                },
                {
                    code:'paramsSetting',
                    name:'参数设置',
                    index:8,
                    url:'/home/paramsSetting'
                },
                {
                    code:'ngSetting',
                    name:'NG属性',
                    index:9,
                    url:'/home/ngSetting'
                },
            ],
            'engineer':[
                {
                    code:'personnelManage',
                    name:'人员管理',
                    index:2,
                    url:'/home/personnelManage'
                },
                {
                    code:'materialInfo',
                    name:'物料信息',
                    index:4,
                    url:'/home/materialInfo'
                },
                {
                    code:'productManage',
                    name:'产品管理',
                    index:5,
                    url:'/home/productManage'
                },
                {
                    code:'materialManage',
                    name:'工站管理',
                    index:6,
                    url:'/home/materialManage'
                },
                {
                    code:'paramsSetting',
                    name:'参数设置',
                    index:8,
                    url:'/home/paramsSetting'
                },
            ],
            'operator':[
                {
                    name:'产品1',
                    index:1,
                    url:'/home/index',
                    children:new Array(12).fill({name:'工站1'})
                },
                {
                    name:'产品2',
                    index:2,
                    url:'/home/index',
                    children:new Array(2).fill({name:'22'})
                },
            ]
        };
        state.allMenus = mapMenus[currentRole && currentRole.id || 'operator'];
    },
    setCurrentMenu(state,currentMenu){
        state.currentMenu = currentMenu;
    }
};

export default {
    state,
    getters,
    actions,
    mutations
}
